package com.itboye.ihomebank.activity.me;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.activity.login.TimeCount;
import com.itboye.ihomebank.presenter.UserPresenter;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.MySmsObserver;
import com.itboye.ihomebank.util.SPUtils;

import java.util.Observable;
import java.util.Observer;

/**
 * 忘记旧密码
 */
public class ActivityWangJiUserPass extends BaseOtherActivity implements Observer{
    View v_statusbar;
    TextView add_shap_title_tv;
    ImageView close_icon;

    TextView wangji_pass_phone;//手机号
    TextView wangji_pass_send;//发送验证码
    EditText wangji_pass_yzm;
    TextView pass_one_ok;//下一步

    String mobile;//手机号
    private TimeCount time;// 倒计时

    UserPresenter userPresenter;
    private MySmsObserver mySmsObserver;
    public static final int MSG_RECEIVER_CODE = 1;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MSG_RECEIVER_CODE:
                    String message = (String) msg.obj;
                    wangji_pass_yzm.setText(message);
                    wangji_pass_yzm.setSelection(message.length());
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    protected int layout_id() {
        return R.layout.activity_wang_ji_user_pass;
    }

    @Override
    protected void onInitController() {

    }

    @Override
    protected void onInit() {
        setStatusBarHeight(v_statusbar);

        add_shap_title_tv.setText("修改密码");

        userPresenter = new UserPresenter(this);
        mobile = (String) SPUtils.get(this, null, SPContants.MOBILE, "");
        if (!mobile.equals("")) {
            try {
                String hintphone = mobile.substring(0, 3) + "****"
                        + mobile.substring(7, 11);
                wangji_pass_phone.setText("请输入" + hintphone + "收到的短信校验码");
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            wangji_pass_phone.setText("");
        }
        mySmsObserver = new MySmsObserver(this, handler);
        Uri uri = Uri.parse("content://sms");
        getContentResolver().registerContentObserver(uri, true, mySmsObserver);
    }

        @Override
        protected void onDestroy () {
            super.onDestroy();
            getContentResolver().unregisterContentObserver(mySmsObserver);
        }

    @Override
    protected void onReadIntent(Bundle bundle) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.close_icon:
                finish();
                break;
            case R.id.wangji_pass_send://发送验证码
                userPresenter.send(mobile, 2 + "");
                time = new TimeCount(60000, 1000, wangji_pass_send);// 构造CountDownTimer对象
                time.start();
                break;
            case R.id.pass_one_ok:
                userPresenter.verification( mobile, 2 + "", wangji_pass_yzm.getText().toString());

                break;

            default:
                break;
        }
    }

    @Override
    public void update(Observable observable, Object data) {
        ResultEntity entity = handlerError(data);
        if (entity != null) {
            if (entity.getEventType() == UserPresenter.send_success) {
                ByAlert.alert(entity.getData());
            }
            if (entity.getEventType() == UserPresenter.send_fail) {
                ByAlert.alert(entity.getData());
            }
            if (entity.getEventType() == UserPresenter.verification_success) {
                ByAlert.alert(entity.getData());
                Intent intent=new Intent(this,ActivityWangJiUserPassTwo.class);
                startActivity(intent);
            }
            if (entity.getEventType() == UserPresenter.verification_fail) {
                ByAlert.alert(entity.getData());
            }
        }
    }
}
